import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.1
import PacketCap 1.0

ColumnLayout
{
    anchors.fill: parent
    anchors.topMargin: 20
    anchors.leftMargin: 20
    anchors.bottomMargin: 20
    anchors.rightMargin: 20
    spacing: 20

    property alias devsCbBox: m_devsCbBox
    property alias destMac: m_destMac
    property alias sourceMac: m_sourceMac
    property alias destIp: m_destIp
    property alias sourceIp: m_sourceIp
    property PCapSender m_sender: PCapSender {
        handle: PCapHandle {
            id: m_handle
        }
    }
    property PCapPacket m_packet: PCapPacket {
        PCapEthFrame {
            id: m_ethFrame
            type: "0x86dd"
        }
        PCapIpv6Packet {
            id: m_ipPacket
        }
        CPCapPktData {
            id: m_data
        }
    }

    Rectangle {
        color: "transparent"
        Layout.fillHeight: true
        Layout.fillWidth: true
        ComboBox {
            id: m_devsCbBox
            width: parent.width
            height: parent.height/2
            x: 0
            y: height/2
            font.pixelSize: g_fontSize
            model: ListModel {}
        }
    }

    RowLayout {
        Layout.fillHeight: true
        Layout.fillWidth: true
        LineInput {
            id: m_sourceMac
            Layout.fillHeight: true
            Layout.fillWidth: true
            textCtrl.text: qsTr("Source MAC:")
            textCtrl.font.pixelSize: g_fontSize
            inputCtrl.font.pixelSize: g_fontSize
        }
        LineInput {
            id: m_destMac
            Layout.fillHeight: true
            Layout.fillWidth: true
            textCtrl.text: qsTr("Dest. MAC:")
            textCtrl.font.pixelSize: g_fontSize
            inputCtrl.font.pixelSize: g_fontSize
        }
    }

    RowLayout {
        Layout.fillHeight: true
        Layout.fillWidth: true
        LineInput {
            id: m_sourceIp
            Layout.fillHeight: true
            Layout.fillWidth: true
            textCtrl.text: qsTr("Source IP:")
            textCtrl.font.pixelSize: g_fontSize
            inputCtrl.font.pixelSize: g_fontSize
        }
        LineInput {
            id: m_destIp
            Layout.fillHeight: true
            Layout.fillWidth: true
            textCtrl.text: qsTr("Dest. IP:")
            textCtrl.font.pixelSize: g_fontSize
            inputCtrl.font.pixelSize: g_fontSize
        }
    }

    LineInput {
        id: m_packetData
        Layout.fillHeight: true
        Layout.fillWidth: true
        textCtrl.text: qsTr("Packet Data")
        textCtrl.font.pixelSize: g_fontSize
        inputCtrl.font.pixelSize: g_fontSize
    }

    Rectangle {
        color: "transparent"
        Layout.fillHeight: true
        Layout.fillWidth: true
        Button {
            width: parent.width/6
            height: parent.height/2
            x: parent.width - width
            y: height/2
            text: qsTr("Send")
            font.pixelSize: g_fontSize
            onClicked: {
                if (devsCbBox.currentIndex >= 0) {
                    m_ethFrame.destMac = destMac.inputCtrl.text
                    m_ethFrame.sourceMac = sourceMac.inputCtrl.text
                    m_ipPacket.destIp = destIp.inputCtrl.text
                    m_ipPacket.sourceIp = sourceIp.inputCtrl.text

                    m_sender.open(g_pcapHost.devs[devsCbBox.currentIndex])
                    m_sender.sendPacket(m_packet)
                    m_sender.close()
                }
            }
        }
    }

    Component.onCompleted: {
        var devs = g_pcapHost.devs
        for (var i = 0; i < devs.length; i++) {
            m_devsCbBox.model.append({text: devs[i].source})
        }
        m_devsCbBox.currentIndex = 0
    }
}
